<template>
  <div class="login">
    <!-- <div class="contenText">电力培训 视频教学辅助系统</div> -->
    <div class="conte">
      <div class="cont_left">
        <img src="@/assets/images/u20.png" alt="logo" />
        <p>培训中心</p>
        <span> Copyright © 郑州三和视讯股份有限公司 </span>
      </div>
    </div>
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="title">视频裁判教学辅助系统</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="on"
          placeholder="账号"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="on"
          placeholder="密码"
          @keyup.enter.native="logins"
        >
        </el-input>
      </el-form-item>
      <!-- <el-form-item prop="code">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="validCode"
            class="el-input__icon input-icon"
          />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item> -->
      <el-checkbox
        v-model="loginForm.rememberMe"
        style="margin: 0px 0px 25px 0px"
      >
        记住密码
      </el-checkbox>
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click.native.prevent="logins"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getUserName } from "@/api";
import Qs from "qs";
// import { mapMutations } from "vuex";
export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
      },
      userToken: "",
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
      },
      loading: false,
      redirect: undefined,
    };
  },
  methods: {
    // ...mapMutations(["changeLogin"]),
    logins() {
      // this.$router.push("controller");
      if (!this.loginForm.username || !this.loginForm.password) {
        this.$message.warning("账号或密码不能为空");
      } else {
        console.log(this.loginForm);
        this.$axios({
          method: "post",
          url: " /login",
          data: Qs.stringify(this.loginForm),
        })
          .then((res) => {
            if (res.data.code == 200) {
              this.userToken = res.data.data.token;
              // 将用户token保存到vuex中
              // _this.changeLogin({ Authorization: _this.userToken });
              // this.$store.commit("changeLogin",{ Authorization: this.userToken })
              localStorage.setItem("Authorization", this.userToken);
              getUserName({ usersAccount: this.loginForm.username })
                .then((res) => {
                  console.log(res.data);
                  this.$store.commit("setusers", res.data);
                  // localStorage.setItem("usersRole",res.data.usersRole);
                  this.$store.commit("setRole", res.data.usersRole);
                  this.$store.commit("setName", this.loginForm.username);
                  localStorage.setItem("Namea", this.loginForm.username);
                  localStorage.setItem("usersId", res.data.usersId);
                  localStorage.setItem("usersName", res.data.usersName);
                  setTimeout(() => {
                    this.$router.push("/Switchtest");
                    this.$message({
                      message: "登录成功",
                      type: "success",
                    });
                  }, 100);
                })
                .catch((err) => {
                  this.$message.error("获取用户信息错误" - err);
                });

              //
            } else if (res.data == 202) {
              this.$message.error("账号或密码错误!");
            }
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background-image: linear-gradient(to right, #00cdac, #02aab0);
  background: url(../assets/login-background.png) no-repeat;
  background-size: cover;
  --hegt: 220px;
}
.title {
  margin: 0px auto 25px auto;
  font-size: 28px;
  text-align: center;
  color: #707070;
}
.login-form {
  position: fixed;
  top: 52%;
  left: 78%;
  transform: translate(-50%, -50%);
  border-radius: 6px;
  width: 300px;
  // box-shadow: 0 0 25px #10344e;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
  }
  input {
    height: 38px;
  }
}
.conte {
  width: 360px;
  position: fixed;
  top: 52%;
  left: 75%;
  transform: translate(-50%, -50%);
  height: 0px;
  background: rgb(255, 255, 255);
  border-top: 300px solid transparent;
  border-left: 100px solid #005957;
  .cont_left {
    position: absolute;
    top: -300px;
    width: var(--hegt);
    left: -320px;
    height: 300px;
    background: #005957;
    color: #fff;
    img {
      width: 80px;
      margin: 25px auto 15px 20px;
      padding-bottom: 15px;
    }
    p {
      margin-left: 29px;
      font-size: 18px;
      font-weight: bold;
      padding: 15px;
      border-top: 2px solid #fff;
    }
    span {
      font-size: 12px;
      position: absolute;
      width: 100%;
      bottom: 20px;
      left: 20px;
    }
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 20px;
  position: fixed;
  bottom: 40px;
  width: 100%;
  text-shadow: 0 0 2px black;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
